#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>

void SetNonBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if(fl < 0)
    {
        perror("fcntl");
        return;
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}

int main()
{
    SetNonBlock(0);
    while(true)
    {
        char buffer[1024];
        int n = read(0, buffer, 1023);
        if(n > 0)
        {
            buffer[n] = 0;
            std::cout << "echo:" << buffer << std::endl;
        }
        else if (n == 0)
        {
            std::cout << "read EOF" << std::endl;
        }
        else
        {
            if(errno == EWOULDBLOCK)
                std::cout << "读事件没就绪" << std::endl;
            else if(errno == EINTR)
                std::cout << "被信号中断" << std::endl;
            else
                std::cout << "读错误" << std::endl;
        }
        sleep(1);
    }
    return 0;
}